package com.jack.bookshop.servlet;

import com.google.gson.Gson;
import com.jack.bookshop.dao.BookDao;
import com.jack.bookshop.dao.CartDao;
import com.jack.bookshop.vo.BookVo;
import com.jack.bookshop.vo.CartVo;
import com.jack.bookshop.vo.ResultVo;
import com.jack.bookshop.vo.UserVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/cart.do")
public class CartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    //
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        UserVo userVo = (UserVo) request.getSession().getAttribute("userVo");
        String msg = "";
        String title = "";
        int i = 0;
        // 添加到购物车
        if (action.equals("add")) {
            String bookIdStr = request.getParameter("bookId");
            int bookId = 0;
            try {
                bookId = Integer.parseInt(bookIdStr);
            } catch (Exception e) {
                System.out.println("Book Id is not a number!");
            }
            i= this.insertOne(userVo.getId(), bookId);
            if(i==1) {
                title = "SUCCESS";
                msg = "Added to cart successfully !";
            }else {
                title = "ERROR";
                msg = "Failed to add to cart, please try again !";
            }
        }
        // 更新数量
        if (action.equals("update")) {
            int cartId = Integer.parseInt(request.getParameter("id"));
            int quantity = Integer.parseInt(request.getParameter("quantity"));
            i = new CartDao().updateQuantity(cartId, quantity);
            if(i==1) {
                title = "SUCCESS";
                msg = "Updated quantity successfully !";
            }else {
                title = "ERROR";
                msg = "Failed to update quantity, please try again !";
            }
        }
        // 更新选中状态
        if (action.equals("checked")) {
            int cartId = Integer.parseInt(request.getParameter("id"));
            int checked = Integer.parseInt(request.getParameter("checked"));
            i = new CartDao().updateChecked(cartId,checked);
            if(i==1) {
                title = "SUCCESS";
                msg = "Set checked successfully !";
            }else {
                title = "ERROR";
                msg = "Failed to set checked, please try again !";
            }
        }
        // 删除图书
        if (action.equals("delete")) {
            int BookId = Integer.parseInt(request.getParameter("id"));
            i = new CartDao().deleteOne(BookId);
            if(i==1) {
                title = "SUCCESS";
                msg = "Remove successfully !";
            }else {
                title = "ERROR";
                msg = "Failed to Remove, please try again !";
            }
        }
        response.sendRedirect("cart.jsp?title="+title+"&msg="+msg);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

    public int insertOne(int userId, int bookId) {
        int i = 0;
        BookVo bookVo = new BookDao().getOneById(bookId);
        CartVo cartVo = new CartVo();
        cartVo.setUser_id(userId);
        cartVo.setBook_id(bookId);
        cartVo.setQuantity(1);
        cartVo.setChecked(1);
        cartVo.setCreate_gmt(System.currentTimeMillis());
        cartVo.setBook_title(bookVo.getTitle());
        cartVo.setBook_image_url(bookVo.getImage_url());
        cartVo.setBook_price(bookVo.getPrice());
        i = new CartDao().insertOne(cartVo);
        return i;
    }

}


